package util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 类型判别和转换器 专门负责类型转换
 * 
 * @author WUJA13
 * 
 */
public class ParseUtil {

	private DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	private static ParseUtil sInstance;

	private ParseUtil() {

	}

	public static ParseUtil getInstance() {
		if (sInstance == null) {
			synchronized (ParseUtil.class) {
				if (sInstance == null)
					sInstance = new ParseUtil();
			}
		}
		return sInstance;
	}

	// /String -> Double
	public Double string2Double(String value) throws NumberFormatException {
		if (isNull(value))
			return null;
		return Double.parseDouble(value);
	}

	// /String -> Boolean
	public Boolean string2Boolean(String value) throws NumberFormatException {
		if (isNull(value))
			return null;
		return Boolean.parseBoolean(value);
	}

	// /String -> Date
	public Date string2Date(String value) throws ParseException {
		if (isNull(value))
			return null;
		return format.parse(value.trim());
	}

	// /String 是否为空
	public boolean isNull(String value) {
		return (value == null || value.length() == 0 || value.trim().length() == 0);
	}
}
